const http = require('http');
const fs = require('fs');

let sever = http.createServer();
let port = 8081;

sever.on('request', (req, res) => {
  switch (req.url) {
    case '/':
      res.setHeader("Content-Type", "text/html;charset=utf8");
      res.write('<h1><mark>首页</mark></h1>')
      res.end();
      break;
    case '/login.html':
      fs.readFile('../login.html', 'utf8', (err, data) => {
        if(err) {
          throw err
        }else {
          res.write(data)
          res.end();
        }
      })
      break;
    case '/node.jpg':
      fs.readFile('../node.jpg', (err, data) => {
        if(err) {
          throw err
        }else {
          res.end(data);
        }
      })
      break;
    case '/login.css':
      fs.readFile('../login.css', 'utf8', (err, data) => {
        if(err) {
          throw err
        }else {
          res.end(data);
        }
      })
      break;
      case '/login.js':
        fs.readFile('../login.js', 'utf8', (err, data) => {
          if(err) {
            throw err
          }else {
            res.end(data);
          }
        })
        break;
    default:
      break;
  }
})

sever.listen(port, '127.0.0.1', () => {
  console.log(`服务已启动，正在监听${port}端口！`)
})